﻿using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Flock/Behavior/Alignment")]
public class AlignmentBehavior : FlockBehavior
{
    public override Vector2 Force(FlockItem item, List<Transform> neighbors)
    {
        Vector2 force = Vector2.zero;

        int count = neighbors.Count;
        if (count == 0) return force;

        for (int i = 0; i < count; i++)
        {
            Transform neighborTF = neighbors[i];
            force += (Vector2) neighborTF.up;
        }

        force /= count;

        // 产生的力由向量的差量决定，所以得到目标朝向后，需要减去当前的朝向
        force -= (Vector2) item.transform.up;

        return force;
    }
}